package com.mutual.assistance.user.mq.config;

import com.mutual.assistance.user.constant.MqConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author chenyuxin
 * @version 1.0
 * @description 消费者消息处理失败处理
 * @date 2023/4/29 22:26
 */
@Configuration
public class ErrorMessageConfig {

    @Bean
    public DirectExchange errorMessageExchange(){
        return new DirectExchange(MqConstant.ERROR_EXCHANGE);
    }

    @Bean
    public Queue errorQueue(){
        return new Queue(MqConstant.ERROR_QUEUE, true);
    }

    @Bean
    public Binding errorBinding(Queue errorQueue, DirectExchange errorMessageExchange){
        return BindingBuilder.bind(errorQueue).to(errorMessageExchange).with(MqConstant.USER_ERROR_ROUTING_KEY);
    }

    @Bean
    public MessageRecoverer republishMessageRecoverer(RabbitTemplate rabbitTemplate){
        return new RepublishMessageRecoverer(rabbitTemplate, MqConstant.ERROR_EXCHANGE, MqConstant.USER_ERROR_ROUTING_KEY);
    }
}
